var gulp = require("gulp"),
    connect = require("gulp-connect"),//页面刷新
    sass = require("gulp-sass"),
    bable = require("gulp-babel"),//js编译器，把es6编译成es5;
    uglify = require("gulp-uglify"),//js压缩
    cssmin = require('gulp-cssmin');//css压缩
    proxy = require('http-proxy-middleware');
//开启服务器：
sass.compiler = require("node-sass");

gulp.task("connect", async () => {
    return connect.server({
        root: "dist",
        livereload: true,
        port: 3000 ,
        middleware : function(){
            return [
                  // 表示在地址栏里面输入的东西是什么 : 
                  proxy('/ning',  {
                      target: 'https://store.lining.com/shop/cdns/goods-AGCQ137_2,AGCQ127_1,AGCQ158_4,AGLP161_3,AYMP201_3,AYMP161_4,AWDP819_10,AWDP667_4,AWDQ248_2,AWDP817_2,AWDPC65_3,AYMP231_2,AYMP157_2,AYMP199_1,AYMP197_3,AYMP144_2,AKLP749_1,AKLP593_2,AKLP673_2,AYKP066_4,AKLP626_2,ARLQ005_2,AGCQ176_1,AREQ003_1,AGCQ115_3,AGLP079_4,AGLP145_3,ABAP055_1,AGCQ163_1,ARHN075_7,AGCP221_5,ABAP081_1,ARHP253_4,AGLP086_2,AGLP094_4,ARHP262_4,AREP024_2,AGCP128_4,AWDQ252_1,AWDQ254_1,AWDQ077_2,AWDQ101_2,AWDPB03_2,AWDQ039_1,AWDP442_5,AWDPD59_1,AKLQ141_3,AKLQ062_1,AWDQ139_3,AWEP025_2,AWDQ441_1,AWDPD31_1,AWDQ097_4,AWDQ099_1,AWDQ175_1,AWDQ001_1,AWDQ127_3,AWDQ053_1,AKLQ081_1,AKLQ003_1,AKLQ203_1,AKLQ075_1,AKLPA57_3,AWDQ132_2,AWDQ262_1,AWDQ122_2,AWDQ004_2,AWDQ136_2,AWDQ108_3,AWDQ094_5,AWDP652_2,AWDQ088_4,AWDQ266_8,AKLQ096_1,AKLQ102_1,AKXQ036_1,AKLQ002_1,AKLQ076_1,AYMP207_2,AYMP156_1,AYMP148_6,AYMP101_3,AYMP077_2,AYMP158_1,AMRP011_3,AMRP031_1,AMRP023_4,AJMP063_1,AYMP147_1,AYMP043_2,AWBP043_1,LYMP007_2,AYMP053_2,AYMP113_3,AFMP009_2,AYMP087_1,AYMP041_1,AYMP005_2,AFMP029_1,AMRP006_3,AMRP024_2,AMRP022_1,AYMP082_3,AYMP044_7,AYMP086_3,AYMP052_1,AYMP162_3,AYMP032_5,LYMP002_1,AJMP548_1,AYMP066_2,AYMP152_1,AYMP154_1,AYMP058_3,ARLQ005_4,ARHQ053_5,ARHQ064_5,AREQ001_3,AREQ012_2,ARDQ005_3.js',//代理的目标地址
                      changeOrigin:true,
                      pathRewrite:{//路径重写规则 
                          '^/ning':''
                      }
                  }),
              ]
      }
    });
});
gulp.task("html", async () => {
    return gulp.src(["./src/*.html"])
        //转存目标(html)
        .pipe(gulp.dest("./dist/"))
        // 页面自动刷新
        .pipe(connect.reload());
});

gulp.task("sass", async () => {
    return gulp.src(["./src/styles/*.scss"])
        .pipe(sass().on("error", sass.logError))//sass出错时打印错误
        .pipe(gulp.dest("./dist/styles"))//转存目标(css)
        .pipe(connect.reload())//自动刷新

})
gulp.task("sass-build", async () => {
    await gulp.src(["./src/styles/*.scss"])
        .pipe(sass().on('error', sass.logError))
        .pipe(cssmin())//压缩css
        .pipe(gulp.dest("./dist/styles/"))
        
})


gulp.task("javascripts-json", async () => {
    return gulp.src(["./src/javascripts/**/*"])
            .pipe(gulp.dest("./dist/javascripts"))
})




gulp.task("javascripts-libs", async () => {
    await gulp.src(["./src/javascripts/**/*.js"])
          .pipe(gulp.dest("./dist/javascripts"))
})
gulp.task("javascripts", async () => {
    await gulp.src(["./src/javascripts/*.js"])
        .pipe(bable({
            presets: ['@bablel/env']//把es6编译成es5;
        }))
        .pipe(uglify())//js压缩
        .pipe(gulp.dest("./dist/javascripts"))
        
})
gulp.task("javascripts-dev", async () => {
    await gulp.src(["./src/javascripts/*.js"])
        .pipe(gulp.dest("./dist/javascripts"))//转存(js)
        .pipe(connect.reload())
})
gulp.task("watch", async () => {
    await gulp.watch("./src/*.html", gulp.series("html"))//当html发生改变时执行"html"命令
    await gulp.watch("./src/styles/*.scss", gulp.series("sass"))
    await gulp.watch("./src/javascripts/*.js",gulp.series("javascripts-dev"))
})
gulp.task("default", gulp.parallel("watch", "connect","javascripts-libs","javascripts-json"))

gulp.task("build" , gulp.series("javascripts" , "sass-build"))